package com.run.guifeng.controller.admin;

import com.run.guifeng.model.entity.AdminUser;
import com.run.guifeng.model.enums.ServiceResultEnum;
import com.run.guifeng.service.AdminUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * @author sky
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {

    private final AdminUserService adminUserService;

    public AdminController(AdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    @PostMapping(value = "/login")
//    @ApiOperation("Login")
    public void login(@RequestParam("userName") String userName,
                        @RequestParam("password") String password,
                        @RequestParam("verifyCode") String verifyCode,
                        HttpSession session) {
        /*
        if (StringUtils.isEmpty(verifyCode)) {
            session.setAttribute("status", false);
            session.setAttribute("errorMsg", "验证码不能为空");
        }
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            session.setAttribute("status", false);
            session.setAttribute("errorMsg", "用户名或密码不能为空");
        }
        */
        String kaptchaCode = session.getAttribute("verifyCode") + "";
        if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
            session.setAttribute("errorMsg", "验证码错误");
        }
        AdminUser adminUser = adminUserService.login(userName, password);
        if (adminUser != null) {
            session.setAttribute("userName", adminUser.getUserName());
            session.setAttribute("id", adminUser.getId());
            //session过期时间设置为7200秒 即两小时
            //session.setMaxInactiveInterval(60 * 60 * 2);
        } else {
            session.setAttribute("errorMsg", "登陆失败");
        }
    }

    /*
    @GetMapping("/profile")
    @ApiOperation("Profile")
    public String profile(HttpServletRequest request) {
        Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
        AdminUser adminUser = adminUserService.getUserDetailById(loginUserId);
        if (adminUser == null) {
            return "admin/login";
        }
        request.setAttribute("path", "profile");
        request.setAttribute("loginUserName", adminUser.getUserName());
        return "admin/profile";
    }
     */

    @PostMapping("/profile/password")
    @ApiOperation("profile password")
    public String passwordUpdate(HttpServletRequest request, @RequestParam("originalPassword") String originalPassword,
                                 @RequestParam("newPassword") String newPassword) {
        if (StringUtils.isEmpty(originalPassword) || StringUtils.isEmpty(newPassword)) {
            return "参数不能为空";
        }
        Integer loginUserId = (int) request.getSession().getAttribute("id");
        if (adminUserService.updatePassword(loginUserId, originalPassword, newPassword ).equals("success")) {
            //修改成功后清空session中的数据，前端控制跳转至登录页
            request.getSession().removeAttribute("userName");
            request.getSession().removeAttribute("id");
            request.getSession().removeAttribute("errorMsg");
            return ServiceResultEnum.SUCCESS.getResult();
        } else {
            return "修改失败";
        }
    }

    /*
    @PostMapping("/profile/name")
    @ApiOperation("Profile name")
    public String nameUpdate(HttpServletRequest request, @RequestParam("loginUserName") String loginUserName,
                             @RequestParam("nickName") String nickName) {
        if (StringUtils.isEmpty(loginUserName) || StringUtils.isEmpty(nickName)) {
            return "参数不能为空";
        }
        Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
        if (adminUserService.updateName(loginUserId, loginUserName)) {
            return ServiceResultEnum.SUCCESS.getResult();
        } else {
            return "修改失败";
        }
    }
    */

    @GetMapping("/logout")
    @ApiOperation("Admin logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("loginUserId");
        request.getSession().removeAttribute("loginUser");
        request.getSession().removeAttribute("errorMsg");
        return "退出成功";
    }
}
